package cn.seu.together.controllers;

import cn.seu.together.models.Proj;
import cn.seu.together.models.ProjDao;
import cn.seu.together.repository.ProjRepository;
import cn.seu.together.utils.ResponseCode;
import cn.seu.together.utils.ResponseTemplate;
import cn.seu.together.annotation.SerializedField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashSet;

/**
 * Created by corvo on 2/14/17.
 */
@RestController
@RequestMapping("/projs")
public class ProjController {
    @Autowired
    private ProjRepository projRepo;

    @Autowired
    private ProjDao projDao;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResponseTemplate> getParties() {

        //return new ResponseEntity<>(projRepo.findAll(), HttpStatus.OK);
        ResponseTemplate<Collection<Proj>> res = new ResponseTemplate<>(
                projRepo.findAll(),
                ResponseCode.SUCCESS);

        return new ResponseEntity<>(res, HttpStatus.OK);
    }

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @SerializedField(includes = {"phone", "name"}, encode = false)
    public ResponseEntity<Collection<Proj>> getAll() {
        Collection<Proj> projs = new HashSet<>();
        try {
            projs = projDao.getAll();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity<Collection<Proj>>(projs, HttpStatus.OK);
    }

    @RequestMapping(value = "/query_by_id", method = RequestMethod.POST, params = "id")
    public ResponseEntity<ResponseTemplate> getTeamById(@RequestParam("id") long id) {
        Collection<Proj> projs = new HashSet<>();

        ResponseTemplate<Proj> res = new ResponseTemplate<>(
                projDao.getById(id),
                ResponseCode.SUCCESS);

        return new ResponseEntity<>(res, HttpStatus.OK);
    }
}
